import router from "./router";
import nprogress from 'nprogress'
import { ElMessageBox } from "element-plus";
// 引入进度条样式
import 'nprogress/nprogress.css'
nprogress.configure({ showSpinner: false })
import { useUserStore } from "./stores/user";
import { GET_TOKEN } from "./utils/token";
// 前置守卫
router.beforeEach(async (to: any, from: any, next: any) => {
    const userStore = useUserStore()
    nprogress.start()
    const token = GET_TOKEN()
    const username = userStore.username
    if (token) {
        if (to.path == '/login') {
            next({ path: '/' })
        }
        else {
            if (username) {
                next()
            } else {
                ElMessageBox.alert('token已过期', '提示', {
                    confirmButtonText: '返回登录',
                    type: 'warning',
                    draggable: true
                })
                next({ path: '/login', query: { redirect: to.path } })
            }
        }
    } else {
        if (to.path == '/login' || to.path == '/register') {
            next()
        } else {
            next({ path: '/login', query: { redirect: to.path } })
        }
    }
})
router.afterEach((to: any, from: any) => {
    document.title = `${'EduPanda'}-` + to.meta.title
    nprogress.done()
})